﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Services;
using DCS.ComLib.Config;
using VietThuongLMS.Utils;
using VietThuongLMS.Areas.Admin.Models;
using System.IO;

namespace VietThuongLMS.Areas.Admin.Controllers
{
    public class ChiNhanhController : Controller
    {
        //
        // GET: /Admin/ChiNhanh/

        //[Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            if (!Request.IsAuthenticated)
                return RedirectToAction("Login", "Home", new { area = "Admin" });

            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            ChiNhanhViewModel ChiNhanhList = ChiNhanhService.GetChiNhanhList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "ChiNhanh", "Index", "?p=", ChiNhanhList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(ChiNhanhList);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Detail(int id)
        {
            ChiNhanh ChiNhanh = ChiNhanhService.GetChiNhanhById(id);
            return View(ChiNhanh);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(ChiNhanhModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    ChiNhanh data = new ChiNhanh();
                    data.TenChiNhanh = model.TenChiNhanh;
                    data.DiaChi = model.DiaChi;
                    data.MoTa = model.MoTa;
                    //url
                    if (model.UrlAvatar != null && !model.UrlAvatar.FileName.Equals(""))
                    {
                        if (model.UrlAvatar.ContentLength > 2097152)
                        {
                            ModelState.AddModelError("strUrlAvatar", "(*) Hình ảnh 1 không được phép quá 2MB");
                            return View("Create");
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar = fileName;
                        }
                    }
                    if (model.UrlAvatar2 != null && !model.UrlAvatar2.FileName.Equals(""))
                    {
                        if (model.UrlAvatar2.ContentLength > 2097152)
                        {
                            ModelState.AddModelError("strUrlAvatar", "(*) Hình ảnh 2 không được phép quá 2MB");
                            return View("Create");
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar2.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar2, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar2 = fileName;
                        }
                    }
                    if (model.UrlAvatar3 != null && !model.UrlAvatar3.FileName.Equals(""))
                    {
                        if (model.UrlAvatar3.ContentLength > 2097152)
                        {
                            ModelState.AddModelError("strUrlAvatar", "(*) Hình ảnh 3 không được phép quá 2MB");
                            return View("Create");
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar3.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar3, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar3 = fileName;
                        }
                    }
                    if (model.UrlAvatar4 != null && !model.UrlAvatar4.FileName.Equals(""))
                    {
                        if (model.UrlAvatar4.ContentLength > 2097152)
                        {
                            ModelState.AddModelError("strUrlAvatar", "(*) Hình ảnh 4 không được phép quá 2MB");
                            return View("Create");
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar4.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar4, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar4 = fileName;
                        }
                    }
                    if (model.UrlAvatar5 != null && !model.UrlAvatar5.FileName.Equals(""))
                    {
                        if (model.UrlAvatar5.ContentLength > 2097152)
                        {
                            ModelState.AddModelError("strUrlAvatar", "(*) Hình ảnh 5 không được phép quá 2MB");
                            return View("Create");
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar5.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar5, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar5 = fileName;
                        }
                    }
                    /////////////////////

                    bool result = ChiNhanhService.InsertChiNhanh(data);
                    if (result)
                        return RedirectToAction("Index", "ChiNhanh", new { area = "Admin" });
                    return View();
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

        //delete Account
        //[Authorize(Roles = "Admin")]
        [Authorize(Roles = "Admin")]
        [HttpGet]
        public ActionResult Delete(int id)
        {
            bool result = ChiNhanhService.DeleteChiNhanh(id);
            if (result)
            {
                return RedirectToAction("Index", "ChiNhanh", new { area = "Admin" });
            }
            else
            {
                return View();
            }

        }

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            ChiNhanh chiNhanh = ChiNhanhService.GetChiNhanhById(id);
            ChiNhanhModel model = (new ChiNhanhConverter()).ChiNhanhToModel(chiNhanh);
            return View(model);
        }

        [Authorize(Roles = "Admin")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(ChiNhanhModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    ChiNhanh data = new ChiNhanh();
                    data.ID = model.ID;
                    data.TenChiNhanh = model.TenChiNhanh;
                    data.DiaChi = model.DiaChi;
                    data.MoTa = model.MoTa;
                    //url
                    if (model.UrlAvatar != null && !model.UrlAvatar.FileName.Equals(""))
                    {
                        if (model.UrlAvatar.ContentLength > 2097152)
                        {
                            ChiNhanh chiNhanh2 = ChiNhanhService.GetChiNhanhById(model.ID);
                            ChiNhanhModel model2 = (new ChiNhanhConverter()).ChiNhanhToModel(chiNhanh2);
                            ModelState.AddModelError("UrlAvatar", "(*) Hình ảnh 1 không được phép quá 2MB");
                            return View("Edit", model2);
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Edit", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Edit", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar = fileName;
                        }
                    }
                    if (model.UrlAvatar2 != null && !model.UrlAvatar2.FileName.Equals(""))
                    {
                        if (model.UrlAvatar2.ContentLength > 2097152)
                        {
                            ChiNhanh chiNhanh2 = ChiNhanhService.GetChiNhanhById(model.ID);
                            ChiNhanhModel model2 = (new ChiNhanhConverter()).ChiNhanhToModel(chiNhanh2);
                            ModelState.AddModelError("UrlAvatar2", "(*) Hình ảnh 2 không được phép quá 2MB");
                            return View("Edit", model2);
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar2.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar2, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar2 = fileName;
                        }
                    }
                    if (model.UrlAvatar3 != null && !model.UrlAvatar3.FileName.Equals(""))
                    {
                        if (model.UrlAvatar3.ContentLength > 2097152)
                        {
                            ChiNhanh chiNhanh2 = ChiNhanhService.GetChiNhanhById(model.ID);
                            ChiNhanhModel model2 = (new ChiNhanhConverter()).ChiNhanhToModel(chiNhanh2);
                            ModelState.AddModelError("UrlAvatar3", "(*) Hình ảnh 3 không được phép quá 2MB");
                            return View("Edit", model2);
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar3.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar3, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar3 = fileName;
                        }
                    }
                    if (model.UrlAvatar4 != null && !model.UrlAvatar4.FileName.Equals(""))
                    {
                        if (model.UrlAvatar4.ContentLength > 2097152)
                        {
                            ChiNhanh chiNhanh2 = ChiNhanhService.GetChiNhanhById(model.ID);
                            ChiNhanhModel model2 = (new ChiNhanhConverter()).ChiNhanhToModel(chiNhanh2);
                            ModelState.AddModelError("UrlAvatar4", "(*) Hình ảnh 4 không được phép quá 2MB");
                            return View("Edit", model2);
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar4.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar4, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar4 = fileName;
                        }
                    }
                    if (model.UrlAvatar5 != null && !model.UrlAvatar5.FileName.Equals(""))
                    {
                        if (model.UrlAvatar5.ContentLength > 2097152)
                        {
                            ChiNhanh chiNhanh2 = ChiNhanhService.GetChiNhanhById(model.ID);
                            ChiNhanhModel model2 = (new ChiNhanhConverter()).ChiNhanhToModel(chiNhanh2);
                            ModelState.AddModelError("UrlAvatar5", "(*) Hình ảnh 5 không được phép quá 2MB");
                            return View("Edit", model2);
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.UrlAvatar5.FileName);
                            DCS.ComLib.File.Log.Write("ChiNhanhController::Create", "", this.Server.MapPath("~/Images/ChiNhanh/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.UrlAvatar5, 320, this.Server.MapPath("~/Images/ChiNhanh/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("ChiNhanhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.UrlAvatar5 = fileName;
                        }
                    }

                    bool result = ChiNhanhService.Edit(data);
                    if (result)
                        return RedirectToAction("Index", "ChiNhanh", new { area = "Admin" });
                    ChiNhanh chiNhanh = ChiNhanhService.GetChiNhanhById(model.ID);
                    ChiNhanhModel model1 = (new ChiNhanhConverter()).ChiNhanhToModel(chiNhanh);
                    return View(model1);
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

    }
}
